package com.copractice.data.dao.hibernate.knowledge;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.copractice.data.dao.hibernate.HibernateDAO;
import com.copractice.data.dao.knowledge.UsersKnowledgeDAO;
import com.copractice.data.entity.knowledge.UsersKnowledge;

/**
 * Implementation of <code>HibernateDAO</code> for <code>UsersKnowledge</code> class using <code>Hibernate</code> 
 * framework.
 * 
 * @author Dmitry Churbanov (dmitry.churbanov@gmail.com)
 * @version 1.0 03/14/2009
 */
public class HibernateUsersKnowledgeDAO extends HibernateDAO<UsersKnowledge, Long> implements UsersKnowledgeDAO {
    
    private static final Logger LOG = LoggerFactory.getLogger(HibernateUsersKnowledgeDAO.class);

    /**
     * @see com.copractice.data.dao.hibernate.HibernateDAO#getPersistenceClass
     */    
    @Override
    protected Class<UsersKnowledge> getPersistenceClass() {
        return UsersKnowledge.class;
    }
    
    @SuppressWarnings("unchecked")
    public UsersKnowledge fetchUserKnowledgeByUserIdAndTagId(Long userId, Long tagId) {
        List<UsersKnowledge> userKnowledgeList = (List<UsersKnowledge>) getSession()
                                                 .getNamedQuery("USERKNOWLEDGE.fetchByUserIdAndTagId")
                                                 .setLong(0, userId).setLong(1, tagId);
        if (userKnowledgeList.size() == 0) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Didn't find knowledge for user = '{}' and tag = '{}'", userId, tagId);
            }
            return null;
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Found knowledge for user = '{}' and tag = '{}': '{}'", 
                          new Object[]{userId, tagId, userKnowledgeList.get(0)});
            }
            return userKnowledgeList.get(0);
        }
    }    

}
